/** Appointment */
package domain.appointment;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONObject;

import domain.doctor.Doctor;
import domain.patient.Patient;
import domain.records.TreatmentRecord;

/** This class contains an appointment
 * @author jessienewman
 * @version 1.0
 * */
public class Appointment implements Serializable{
	
	/** Field doctor. */
	private Doctor doctor;

	/** Field patient. */
	private Patient patient;
	
	/** Field date.	 */
	private Date date;
	
	/** Field id. */
	private Integer id;
	
	/** Field tr. */
	private TreatmentRecord tr;
	
	/** Creates an appointment
	 * @param doctor doctor for appointment
	 * @param patient patient being treated
	 * @param date date and time of appointment
	 * @param tr treatment record to attach to appointment
	 */
	public Appointment(Doctor doctor, Patient patient, Date date, TreatmentRecord tr){
		this.doctor=doctor;
		this.patient=patient;
		this.date=date;
		this.tr = tr;
	}
	
	public Appointment(final JSONObject json) {
		populateFromJSON(json);
	}
	
	/**
	 * Method setAppointmentId.
	 * @param id Integer
	 */
	public void setAppointmentId(Integer id) {
		this.id = id;
	}
	
	/**
	 * Method getAppointmentId.
	 * @return Integer
	 */
	public Integer getAppointmentId() {
		return id;
	}

	/**
	 * Method getDoctor.
	 * @return Doctor
	 */
	public Doctor getDoctor() {
		return doctor;
	}

	/**
	 * Method setDoctor.
	 * @param doctor Doctor
	 */
	public void setDoctor(Doctor doctor) {
		this.doctor = doctor;
	}

	/**
	 * Method getPatient.
	 * @return Patient
	 */
	public Patient getPatient() {
		return patient;
	}

	/**
	 * Method setPatient.
	 * @param patient Patient
	 */
	public void setPatient(Patient patient) {
		this.patient = patient;
	}

	/**
	 * Method getDate.
	 * @return Date
	 */
	public Date getDate() {
		return date;
	}

	/** Gives the date in a string formatted as such: MMMMM dd, yyyy
	
	 * @return string format of date */
	public String getDateString() {
		final SimpleDateFormat sdf = new SimpleDateFormat("MMMMM dd, yyyy");
		return sdf.format(date);
	}
	
	/**
	 * Method setDate.
	 * @param date Date
	 */
	public void setDate(Date date) {
		this.date = date;
	}
	
	/**
	 * Method setTreatmentRecord.
	 * @param tr TreatmentRecord
	 */
	public void setTreatmentRecord(TreatmentRecord tr) {
		this.tr = tr;
	}
	
	/**
	 * Method getTreatmentRecord.
	 * @return TreatmentRecord
	 */
	public TreatmentRecord getTreatmentRecord() {
		return tr;
	}
	
	/**
	 * Method toString.
	 * @return String
	 */
	public String toString() {
		return "Appointment: " + getDateString() + " for " + getPatient().getFullName();
	}
	
	public JSONObject getJSON() {
		final JSONObject json = new JSONObject();
		json.put("appointmentId", id);
		json.put("date", this.date.getTime());
		json.put("doctor", doctor.getJSON());
		json.put("patient", patient.getJSON());
		json.put("treatmentRecord", tr.getJSON());
		return json;
	}
	
	public void populateFromJSON(final JSONObject json) {
		setAppointmentId(json.getInt("appoinmentId"));
		setDate(new Date(json.getLong("date")));
		setDoctor(new Doctor(json.getJSONObject("doctor")));
		setPatient(new Patient(json.getJSONObject("patient")));
		setTreatmentRecord(new TreatmentRecord(json.getJSONObject("treatmentRecord")));
	}
}
